package com.xcfw.dorm.config;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * @program: Websocket
 * @description:
 * @author: Bruse Queen
 * @create: 2018-03-14 11:53
 **/
@Configuration
public class WebSocketConfig extends WebMvcConfigurerAdapter {


    // 部署到tomcat服务器不需要下面

    /**
     * websocket
     *
     * @return websocket拦截器
     */
//    @Bean
//    public ServerEndpointExporter serverEndpointExporter() {
//        return new ServerEndpointExporter();
//    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() throws Exception {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
//        yaml.setResources(new FileSystemResource(ResourceUtils.getURL("classpath:").getPath() + "config.yml"));//File引入
      yaml.setResources(new ClassPathResource("config.yml"));//class引入
        configurer.setProperties(yaml.getObject());
        return configurer;
    }

}